<%+header%>
<h2><a id="content" name="content"><%:Terminal Equiption Information%></a></h2>
<script>  //general function
	function getValue(ID)
	{
		return document.getElementById(ID).value;
	}
	function setDisplay(ID,type)
	{
		if(!type)
		{
			document.getElementById(ID).style.display="none";
		}
		else
		{
			document.getElementById(ID).style.display="inline";
		}
	}
	function setHTML(ID,STR)
	{
		document.getElementById(ID).innerHTML=STR;
	}
	function getHTML(ID,STR)
	{
		return document.getElementById(ID).innerHTML;
	}
	function setValue(ID,val){
		document.getElementById(ID).value=val;	
	}
</script>

<script>
    function timesort (arr)
    {
        var i = 0;
        var j = 0;
        var t = new Object();
        var leng = arr.length;
        for(i = 0;i < leng;i++)
            for( j = 0 ; j < leng-i-1;j++)
                if(arr[j].time < arr[j+1].time)
                {
                    t = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = t;
                } 
            
    }

    function signalStrengthsort (arr)
    {
        var i = 0;
        var j = 0;
        var t = new Object();
        var leng = arr.length;
        for(i = 0;i < leng;i++)
            for( j = 0 ; j < leng-i-1;j++)
                if(arr[j].signalStrength < arr[j+1].signalStrength)
                {
                    t = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = t;
                } 
            
    }
    function clickSortSignalStrength()
    {
        setHTML("ID_Table_dev","<tr><td style='width:35%;'></td><td style='font-size:20px;color:red'><%:please wait a little......%></td></tr>");
        signalStrengthsort(deviceArr);
	  	setHTML("ID_Table_dev","");
	  	Terminal_info();
    }
	var deviceArr;
	function getTerminalDevInfo()
    {
       receive();

	   var callPath='<%=luci.dispatcher.build_url("admin", "wifiscan","TerminalDevInfo")%>';
        
       callPath +="/"+TMArr; 
	   XHR.get(callPath,null,function(x,rval){
	   	       
	  	       if(null == rval){
	   	           deviceArr=new Array();
	   	       }else{
	   	           deviceArr=rval[0];
	   	       }
	   	       if("undefined" == typeof(deviceArr.length)){
	  	    	    deviceArr=new Array();
	  	       }
                timesort(deviceArr);
	   	        setHTML("ID_Table_dev","");
	  	        Terminal_info();
		     }
	   );
	   setHTML("ID_Table_dev","<tr><td style='width:35%;'></td><td style='font-size:20px;color:red'><%:please wait a little......%></td></tr>");
	}
	
	var dev_detail_flag=0;
	function device_detail(nu)
	{
		if(nu == dev_detail_flag){
			if(getHTML("ID_TR_device_"+nu)!= ""){
				setHTML("ID_TR_device_"+nu,"");
				setValue("ID_INPUT_detailButton"+nu,"<%:details%>");
				return;
			}
		}
		setHTML("ID_TR_device_"+dev_detail_flag,"");
		var distance=Math.round(Math.pow(10,Math.abs(parseInt(deviceArr[nu].signalStrength))/(10*4))*100)/100;
		var gTime=deviceArr[nu].time;
		gTime=gTime.substr(0,4)+"-"+gTime.substr(4,2)+"-"+gTime.substr(6,2)+" "+gTime.substr(8,2)+":"+gTime.substr(10,2)+":"+gTime.substr(10,2);
		var detailStr='<td></td><td colspan="4" >'
			+'<table>'
				+'<tr>'
					+'<td width="15%"><%:Source MAC%>:</td>'
					+'<td style="color:red">'+deviceArr[nu].smac+'</td>'
					+'<td  width="40%"></td>'
				+'</tr>'
				+'<tr>'
					+'<td><%:Dest MAC%>:</td>'
					+'<td style="color:red">'+deviceArr[nu].dmac+'</td>'
					+'<td></td>'
				+'</tr>'
					+'<tr>'
					+'<td><%:Internet MAC%>:</td>'
					+'<td style="color:red">'+deviceArr[nu].imac+'</td>'
					+'<td></td>'
				+'</tr>'
				+'</tr>'
					+'<tr>'
					+'<td><%:Request Connection SSID%>:</td>'
					+'<td style="color:red">'+deviceArr[nu].requsetSSID+'</td>'
					+'<td></td>'
				+'</tr>'
				+'<tr>'
					+'<td><%:Device Name%>:</td>'
					+'<td style="color:red">'+deviceArr[nu].devName+'</td>'
					+'<td></td>'
				+'</tr>'
				+'<tr>'
					+'<td><%:location%>:</td>'
					+'<td style="color:red">'+distance+'m</td>'
					+'<td></td>'
				+'</tr>'
				+'<tr>'
					+'<td><%:Acquired Time%>:</td>'
					+'<td style="color:red">'+gTime+'</td>'
					+'<td></td>'
				+'</tr>'
				+'<tr>'
					+'<td colspan="2" style="width:80%"></td>'
					+'<td>'
					  +'<input class="cbi-button" type="button" value="<%:attack%>" onclick="wificrack(\''+deviceArr[nu].signalPath+'\',\''+deviceArr[nu].imac+'\',\''+deviceArr[nu].smac+'\');"></input>'
					/*	+'<input class="cbi-button" type="button" value="<%:location%>" onclick=""></input>'*/
					+'</td>'
				+'</tr>'
			+'</table>'
		+'</td>';	
		setHTML("ID_TR_device_"+nu,detailStr);
		setValue("ID_INPUT_detailButton"+dev_detail_flag,"<%:details%>");
		setValue("ID_INPUT_detailButton"+nu,"<%:return%>");
		dev_detail_flag=nu;
	}
	function clearTerminalInfo()
	{
		var callPath='<%=luci.dispatcher.build_url("admin", "wifiscan","DatabaseClear")%>';
     callPath=callPath+'/STAInfo';
	   XHR.get(callPath,null,function(x,rval){
	   	    getTerminalDevInfo();
		     }
	   );
	}
	function Terminal_info()
	{
		var devstr='<tr>'
				+'<td width="17%"><%:Current Terminal Device%>:</td>'
				+'<td  style="color:red">'+deviceArr.length+'&nbsp;&nbsp;&nbsp;&nbsp;<%:sets%></td>'
				+'<td width="10%"><input class="cbi-button" type="button" value="<%:sort signal strength%>" onclick="clickSortSignalStrength()" /></td>'
				+'<td width="10%"><input class="cbi-button" type="button" value="<%:sort distance%>" onclick="clickSortSignalStrength()" /></td>'
				+'<td  width="5%"><input class="cbi-button" type="button" value="<%:clear%>" onclick="clearTerminalInfo()" /></td>'
			+'</tr>';
		for(var i = 0 ; i < deviceArr.length ; i++)
		{
			devstr+='<tr>'
					+'<td id="ID_TD_dev_'+i+'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%:MAC:%></td>'
					+'<td style="color:red">'+deviceArr[i].smac+'</td>'
					+'<td colspan="2"></td>'
					+'<td><input id="ID_INPUT_detailButton'+i+'" class="cbi-button" type="button" value="<%:details%>" onclick="device_detail('+i+');"></input></td>'
				+'</tr>'
				+'<tr  id="ID_TR_device_'+i+'"></tr>';
		}
		setHTML("ID_Table_dev",devstr);
	}
    function isNull( str ){
        if ( str == "" )
            return true;
        var regu = "^[ ]+$";
        var re = new RegExp(regu);
        return re.test(str);
    }
    var TMArr = "";
    function receive()
    {
        var thisURL = document.URL;
        var paraArr = thisURL.split('?');
        for(var i = 1; i < paraArr.length ; i++)
        {
            if(isNull(paraArr[i]))
                paraArr[i]="nil";
            TMArr +=paraArr[i]+" ";
        }
    } 


</script>
<script>//click
	function wificrack(sigpath,apmac,srcmac)
	{
		  var callPath='<%=luci.dispatcher.build_url("admin", "wifiscan","wificrack")%>';
     callPath=callPath+'/'+sigpath+'/'+apmac+'/'+srcmac;
	   XHR.get(callPath,null,function(x,rval){
	
	   	      getTerminalDevInfo();
		        
		     }
	   );
		 
	}
</script>


<fieldset class="cbi-section">
	<table width="100%" cellspacing="10" id="ID_Table_dev">
	</table>
	<script>getTerminalDevInfo();</script>
</fieldset>
<%+footer%>